package parse.impl;

import java.util.ArrayList;

import java.util.Map;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import model.ConfigModel;
import model.ReadResultModel;
import parse.ParseCore;

public class ParseToXML implements ParseCore {
	private static Map<String, ArrayList<ArrayList<String>>> subListMap;
	@SuppressWarnings("unchecked")
	private static Map<String, String> convertType = (Map<String, String>) ConfigModel.getConfig("convertType");

	@Override
	public Object doParse(ReadResultModel readResultModel, String tableName, int subListType) throws Exception {
		// 填充源文件信息
		ArrayList<ArrayList<String>> mainList = readResultModel.getMainList();
		subListMap = readResultModel.getSubListMap();

		// 开始构造xml
		Document xmlResult = DocumentHelper.createDocument();
		Element transaction = xmlResult.addElement("transaction");
		transaction.addAttribute("code", "");
		transaction.addAttribute("label", "");
		transaction.addAttribute("describe", "");
		Element attributes = transaction.addElement("attributes");
		// 生成主要列表
		for (int i = 1; i < mainList.size(); i++) {
			ArrayList<String> record = mainList.get(i);
			String length;
			// 替换类型关键字
			String type = record.get(4);
			// 替换中文括号为英文括号
			type.replace("（", "(");
			type.replace("）", ")");
			int indexOfLeftBracket = type.indexOf("(");
			int indexOfRightBracket = type.indexOf(")");
			String oldType;
			if (indexOfLeftBracket == -1) {
				oldType = type;
				length = "0";
			} else {
				oldType = type.substring(0, indexOfLeftBracket);
				length = type.substring(indexOfLeftBracket + 1, indexOfRightBracket);
			}
			if (convertType.containsKey(oldType.toUpperCase())) {
				record.set(4, convertType.get(oldType.toUpperCase()));
			} else {
				record.set(4, oldType);
			}
			// record[0：序号 1：字段名 2：注释 3：必选 4：类型及范围]
			Element attribute = attributes.addElement("attribute").addAttribute("name", record.get(1))
					.addAttribute("label", record.get(2)).addAttribute("type", record.get(4));
			if (!"0".equals(length)) {
				attribute.addAttribute("length", length);
			}
			// 生成次要列表
			if ("JsonArray".equals(record.get(4))) {
				generateSubXML(attribute, record.get(1));
			}
		}
		return xmlResult;
	}

	private static void generateSubXML(Element parentNode, String subListName) {
		if (subListMap.containsKey(subListName)) {
			ArrayList<ArrayList<String>> subList = subListMap.get(subListName);
			Element subAttributes = parentNode.addElement("attributes");
			for (int j = 1; j < subList.size(); j++) {
				ArrayList<String> subRecord = subList.get(j);
				String length;
				// 替换类型关键字
				String type = subRecord.get(2);
				// 替换中文括号为英文括号
				type.replace("（", "(");
				type.replace("）", ")");
				int indexOfLeftBracket = type.indexOf("(");
				int indexOfRightBracket = type.indexOf(")");
				String oldType;
				if (indexOfLeftBracket == -1) {
					oldType = type;
					length = "0";
				} else {
					oldType = type.substring(0, indexOfLeftBracket);
					length = type.substring(indexOfLeftBracket + 1, indexOfRightBracket);
				}
				if (convertType.containsKey(oldType.toUpperCase())) {
					subRecord.set(2, convertType.get(oldType.toUpperCase()));
				} else {
					subRecord.set(2, oldType);
				}

				Element subAttribute = subAttributes.addElement("attribute").addAttribute("name", subRecord.get(0))
						.addAttribute("label", subRecord.get(1)).addAttribute("type", subRecord.get(2));
				if (!"0".equals(length)) {
					subAttribute.addAttribute("length", length);
				}
				if ("JsonArray".equals(subRecord.get(2))) {
					generateSubXML(subAttribute, subRecord.get(0));
				}
			}
			return;
		} else
			return;
	}

}
